<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\system;


use app\admin\model\SystemConfig;
use app\admin\model\SystemConfigGroup;
use app\admin\service\TriggerService;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * Class Config
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="系统配置管理")
 */
class Config extends AdminController
{

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemConfig();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $group = $this->request->param('group',null);
        if($group){
            $tem = 'system/config/'.$group;
        }else{
            $tem = '';
        }
        $newgroup = SystemConfigGroup::where('status',1)
            ->order('sort desc')
            ->select()->toArray();
        foreach ($newgroup as $key=>$value){
            $child = Db::name('system_config')
                ->where('group',$value['identification'])
                ->where('status',1)
                ->select()->each(function ($item){
                    if(isset($item['option'])&&$item['option']){
                        $ex = explode(',',$item['option']);
                        $item['option'] = $ex;
                        $ar = [];
                        foreach ($ex as $value){
                            $axa = explode(':',$value);
                            if(count($axa)==2){
                                $ar[$axa[0]] = $axa[1];
                            }else{
                                $ar[$axa[0]] = $axa[0];
                            }
                        }
                        $item['option'] = $ar;
                    }
                    return $item;
                })->toArray();
            $value['child'] = $child;
            $newgroup[$key] = $value;
        }
//        $res = sysconfig('agreement');
//        echo '<pre>';
//        print_r($newgroup);
//        exit;
        return $this->fetch($tem,['group'=>$newgroup]);
    }

    /**
     * @NodeAnotation(title="保存")
     */
    public function save()
    {
        $post = $this->request->post();
//        dump($post);die();
        try {
            foreach ($post as $key => $val) {
                if(is_array($val)){
                    $val = implode('|',$val);
                }
                $this->model
                    ->where('name', $key)
                    ->update([
                        'value' => html_entity_decode($val),
                    ]);
            }
            if(isset($post['group'])&&$post['group']){
                $array = $this->model->where('group',$post['group'])
                    ->where('formtype','switch')
                    ->select()->toArray();
                foreach ($array as $value){
                    if(!isset($post[$value['name']])){
                        $this->model->where('id',$value['id'])
                            ->save(['value'=>0]);
                    }
                }
            }
            TriggerService::updateMenu();
            TriggerService::updateSysconfig();
        } catch (\Exception $e) {
            $this->error('保存失败');
        }
        $this->success('保存成功');
    }
}